package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.dao.imp.ShopDaoImpl;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        //连接数据库
        ShopDao shopDao = new ShopDaoImpl();
        if("shopList".equals(action)){
            Map<String,String> map = new HashMap<>();
            String keyword = req.getParameter("keyword");
            String phone = req.getParameter("phone");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("keyword",keyword);
            map.put("phone",phone);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            //动态获取社区集合信息
            List<Shop> list = shopDao.selectShops(map);
            //获取总条数
            int count = shopDao.selectShopCount(map);
            //创建一个返回值的map集合用来包裹list和count总数
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("count",count);
            //把结果封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        }else if("addOrUpdate".equals(action)){
            String flag = req.getParameter("addOrUpdate");
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            if(shopDescribe.equals("")){
                shopDescribe = "0";
            }
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            if(shopImage.equals("")){
                shopImage = "0";
            }
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");

//            组成对象
            Shop shop = new Shop(
                    shopAddress,
                    shopCity,
                    shopDescribe,
                    shopDistrict,
                    shopImage,
                    shopLatitude,
                    shopLongitude,
                    shopName,
                    shopPhone,
                    shopProvince
            );
//          判断添加还是修改
            if("1".equals(flag)){
                int row = shopDao.insertShop(shop);
                if(row !=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else{
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }else{
                Integer shopId = Integer.valueOf(req.getParameter("shopId"));
                Shop shops = new Shop(
                        shopAddress,
                        shopCity,
                        shopDescribe,
                        shopDistrict,
                        shopImage,
                        shopLatitude,
                        shopLongitude,
                        shopName,
                        shopPhone,
                        shopProvince,
                        shopId
                );

                int row = shopDao.updateShop(shops);
                if(row!=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else {
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
